package com.github.baichuan.gb32960.codec;

import com.github.baichuan.iot.protocol.pdu.Decoder;
import io.netty.buffer.ByteBuf;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 车辆位置数据
 */
public class Position implements Decoder {
    @Override
    public List<Map<String, Object>> decode(ByteBuf buf) {
        Map<String, Object> data = new TreeMap<>();

        int flags = buf.readUnsignedByte();

        // 定位状态
        data.put("status", flags & 0x01);

        // 经度
        int lngSign = (flags & 0x02) == 0 ? 1 : -1;
        data.put("lng", BigDecimal.valueOf(lngSign * buf.readUnsignedInt() * 0.000001).setScale(6, RoundingMode.HALF_UP));

        // 纬度
        int latSign = (flags & 0x04) == 0 ? 1 : -1;
        data.put("lat", BigDecimal.valueOf(latSign * buf.readUnsignedInt() * 0.000001).setScale(6, RoundingMode.HALF_UP));

        return Arrays.asList(data);
    }
}
